import java.util.Collection;
import java.util.Iterator;
import java.util.Map;
import java.io.StringWriter;

import org.json.JSONObject;
import org.json.JSONString;
import org.json.JSONStringer;
import org.json.JSONArray;

public class testJSON
{
    public static void main(String args[])
    {//{{{
        Iterator it;
        JSONArray a;
        JSONObject j;
        JSONStringer jj;
        String s;
        
        try
        {
            s = "{     \"list of lists\" : [         [1, 2, 3],         [4, 5, 6],     ] }";
            j = new JSONObject(s);
            System.out.println(j.toString());
                    
            s = "{ \"entity\": { \"imageURL\": \"\", \"name\": \"IXXXXXXXXXXXXX\", \"id\": 12336, \"ratingCount\": null, \"averageRating\": null } }";
            j = new JSONObject(s);
            System.out.println(j.toString());

            s="{\"uuid\":100,\"account\":\"壮东风\",\"item\":{\"a\":100,\"b\":[\"x\",\"y\",\"z\"]}}";
            j=new JSONObject(s);
            System.out.println(j.toString());
            if(j.has("uuid"))
            {
                int uuid=j.getInt("uuid");
                System.out.println("uuid: "+uuid);
            }
            else
                System.out.println("uuid is unavailable");
            if(j.has("account"))
            {
                String account=j.getString("account");
                System.out.println("account: "+account);
            }
            else
                System.out.println("account is unavailable");
            JSONObject item=j.getJSONObject("item");
            String ia=item.getString("a");
            System.out.println("item=>a: "+ia);
            JSONArray ib=item.getJSONArray("b");
            System.out.print("item=>b:");
            for(int i=0; i<ib.length(); ++i)
            {
                System.out.print(" "+ib.getString(i));
            }
            System.out.println("");

            s="[\"x\",\"y\",\"z\"]";
            a=new JSONArray(s);
            System.out.println(a.toString());

            jj = new JSONStringer();
            s = jj
	            .object()
	                .key("single")
	                .value("MARIE HAA'S")
	                .key("Johnny")
	                .value("MARIE HAA\\'S")
	                .key("foo")
	                .value("bar")
	                .key("baz")
	                .array()
                        .object()
                            .key("quux")
                            .value("Thanks, Josh!")
                        .endObject()
	                .endArray()
	            .endObject()
                .toString();
            System.out.println(s);

            jj = new JSONStringer();
            jj.array();
            jj.value(1);
            jj.array();
            jj.value(null);
            jj.array();
            jj.object();
            jj.key("empty-array").array().endArray();
            jj.key("answer").value(42);
            jj.key("null").value(null);
            jj.key("false").value(false);
            jj.key("true").value(true);
            jj.key("big").value(123456789e+88);
            jj.key("small").value(123456789e-88);
            jj.key("empty-object").object().endObject();
            jj.key("long");
            jj.value(9223372036854775807L);
            jj.endObject();
            jj.value("two");
            jj.endArray();
            jj.value(true);
            jj.endArray();
            jj.value(98.6);
            jj.value(-100.0);
            jj.object();
            jj.endObject();
            jj.object();
            jj.key("one");
            jj.value(1.00);
            jj.endObject();
            jj.value(100);
            jj.endArray();
            System.out.println(jj.toString());
            System.out.println(new JSONArray(jj.toString()).toString(4));
        }
        catch (Exception e)
        {
            System.out.println(e.toString());
        }
    }//}}}
}

class Obj implements JSONString
{//{{{
    public String aString;
    public double aNumber;
    public boolean aBoolean;

    public Obj(String string, double n, boolean b) {
        this.aString = string;
        this.aNumber = n;
        this.aBoolean = b;
    }

    public double getNumber() {
        return this.aNumber;
    }

    public String getString() {
        return this.aString;
    }

    public boolean isBoolean() {
        return this.aBoolean;
    }

    public String getBENT() {
        return "All uppercase key";
    }

    public String getX() {
        return "x";
    }

    public String toJSONString() {
        return "{" + JSONObject.quote(this.aString) + ":" + 
            JSONObject.doubleToString(this.aNumber) + "}";
    }            
    public String toString() {
        return this.getString() + " " + this.getNumber() + " " + 
            this.isBoolean() + "." + this.getBENT() + " " + this.getX();
    }
}      //}}}
